package com.meiyou.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.meiyou.context.BaseContext;
import com.meiyou.dto.StarDto;
import com.meiyou.dto.StarPageDto;
import com.meiyou.entity.Collect;
import com.meiyou.entity.Star;
import com.meiyou.mapper.StarMapper;
import com.meiyou.result.PageResult;
import com.meiyou.service.StarService;
import com.meiyou.vo.ArticleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @BelongsProject: meiyou
 * @BelongsPackage: com.meiyou.service.impl
 * @Author: 尹晓安
 * @Date: 2023/11/25 18:10
 * @Description: todo
 */
@Service
public class StarServiceImp implements StarService {

    @Autowired
    private StarMapper starMapper;

    /**
     * 点赞、取消点赞文章
     *
     * @param starDto
     * @return
     */
    @Override
    public void starOrCancel(StarDto starDto) {

        // 1.查询数据表里是否有该数据，有则删除，无则添加（点赞和取消点赞）
        Star star = new Star();

        star.setArticleId(starDto.getArticleId());
        star.setUserId(BaseContext.getCurrentId());
        // 查询数据表
        Integer count = starMapper.select(star);
        if (count > 0) {
            // 有则删除
            starMapper.delete(star);
            return;
        }
        // 无则添加
        starMapper.add(star);

    }

    /**
     * 获取用户文章被点赞数
     *
     * @return
     */
    @Override
    public Integer getStarNum() {
        Star star = new Star();
        star.setUserId(BaseContext.getCurrentId());
        Integer count = starMapper.select(star);

        return count;
    }
}
